package com.wlanauth.service.radius.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wlanauth.core.exception.ServiceException;
import com.wlanauth.dao.radius.RadAcctDao;
import com.wlanauth.domain.radius.RadAcct;
import com.wlanauth.service.common.impl.CommonServiceImpl;
import com.wlanauth.service.radius.RadAcctService;

public class RadAcctServiceImpl extends CommonServiceImpl<RadAcct> implements RadAcctService {
	
	protected Log logger = LogFactory.getLog(this.getClass());
	
	private static final String TimeSpan = "%Y-%m-%d-%H-%i-%s";//有序的
	
	public List<?> queryByDateGroupAndDateStr(String groupFormat,String conditionFormat, String dateStr)
			throws ServiceException {
		String dateFormat = getPrexfixFullDateFormat(groupFormat,conditionFormat);
		logger.info("查询流量信息,入参:[groupFormat:"+groupFormat+",conditionFormat:"+conditionFormat+",dateStr:"+dateStr+",dateFormat:"+dateFormat+"]");
		
		RadAcctDao dao = (RadAcctDao)this.getDao();
		
		return dao.findByNamedQueryAndNamedParam("com.wlanauth.service.radius.impl.RadAcctServiceImpl.queryByDateGroupAndDateStr",
				new String[]{"dateFormat","conditionFormat","groupFormat","conditionStr"},
				new Object[]{dateFormat,conditionFormat,groupFormat,dateStr});
	}
	public List<?> queryByDateStr(String dateStr,int start,int limit) throws ServiceException {
		String namedQuery = "com.wlanauth.service.radius.impl.RadAcctServiceImpl.queryByDateStr";
		
		String dateFormat = "%Y";
		int len = dateStr.split("-").length;
		switch (len){
			case 1:dateFormat="%Y";break;
			case 2:dateFormat="%Y-%m";break;
			case 3:dateFormat="%Y-%m-%d";break;
			case 4:dateFormat = "%Y-%m-%d-%H";break;
		}
		RadAcctDao dao = (RadAcctDao)this.getDao();

		return dao.findByNamedQueryAndNamedParam(namedQuery, 
				new String[]{"dateFormat","dateStr","start","size"}, 
				new String[]{dateFormat,dateStr,""+start,""+limit});
	}
	private String getPrexfixFullDateFormat(String format1,String format2) {
		String[] s = (""+format1+"-"+format2).split("-");
		ArrayList<String> format = new ArrayList<String>();
		for(String t:s){
			if(t!=null&&TimeSpan.contains(t)&&!format.contains(t)){
				List<String> parents = wrapParent(t);
				for(String p:parents){
					if(!format.contains(p))format.add(p);
				}
			}
		}
		
		Collections.sort(format, new Comparator<String>(){
			private int getLevel(String s){
				return TimeSpan.indexOf(s);
			}
			public int compare(String arg0, String arg1) {
				return getLevel(arg0)-getLevel(arg1);
			}
			
		});
		String formatStr = "";
		for(String t:format){
			formatStr+="-"+t;
		}
		return (formatStr.equals("")?"":formatStr.substring(1));
	}

	private List<String> wrapParent(String t) {
		String[] f = TimeSpan.split("-");
		List<String> list = new ArrayList<String>();
		for(String s:f){
			list.add(s);
			if(s.equals(t)){
				return list;
			}
		}
		return list;
	}

}
